<template>
  <div class="login-container">
    <div class="form-container">
      <div class="avatar_box">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label-width="0px" prop="username">
          <el-input v-model="ruleForm.username" prefix-icon="el-icon-user"></el-input>
        </el-form-item>

        <el-form-item label-width="0px" prop="password">
          <el-input v-model="ruleForm.password" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login('ruleForm')">登录</el-button>
          <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";
import { SET_TOKEN } from "@/util/storage";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          },
          {
            min: 3,
            max: 5,
            message: "长度在 3 到 5 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },

  components: {},

  methods: {
    login(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          login(this.ruleForm.username, this.ruleForm.password).then(res => {
            if (res.meta.status !== 200) return this.$message.error("登录失败！");
            this.$message.success("登录成功");
            SET_TOKEN(res.data.token);
            this.$router.push("/home");
          });
        } else {
          return false;
        }
      });
    },
    resetForm() {}
  }
};
</script>

<style scoped>
.login-container {
  height: 100%;
  background-color: #2a4b6b;
  position: relative;
}

.avatar_box {
  height: 130px;
  width: 130px;
  border-radius: 50%;
  border: 1px solid #eee;
  box-shadow: 0px 0px 10px #ddd;
  overflow: hidden;
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 10px;
}
.avatar_box img {
  width: 100%;
  height: 100%;
  background-color: #eeeeee;
  border-radius: 50%;
}
.form-container {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.el-form {
  margin-top: 120px;
}
.el-form-item {
  margin-left: 20px;
  margin-right: 20px;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
